package com.huawei.admins.platform.gateway.core.config;

import com.huawei.admins.platform.gateway.core.component.filter.GatewayFilterFactory;
import com.huawei.admins.platform.gateway.core.component.filter.GlobalFilter;
import com.huawei.admins.platform.gateway.core.component.predicate.factory.RoutePredicateFactory;
import com.huawei.admins.platform.gateway.core.component.router.RouteDefinitionLocator;
import com.huawei.admins.platform.gateway.core.component.router.RouteFilterHandlerAdapter;
import com.huawei.admins.platform.gateway.core.component.router.RoutePredicateHandlerMapping;
import com.huawei.admins.platform.gateway.core.component.router.RouterLocator;
import com.huawei.admins.platform.gateway.core.component.router.locator.RouteDefinitionRouteLocator;
import com.huawei.admins.platform.gateway.core.component.router.parser.PropertiesRouteDefinitionLocator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.web.reactive.DispatcherHandler;

import java.util.List;

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({DispatcherHandler.class, HttpHandler.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
public class GatewayRouterConfiguration implements EnvironmentAware {

    private Environment environment;

    private final GatewayProperties gatewayProperties;

    public GatewayRouterConfiguration(GatewayProperties gatewayProperties) {
        this.gatewayProperties = gatewayProperties;
    }

    @Bean
    @ConditionalOnMissingBean(PropertiesRouteDefinitionLocator.class)
    public PropertiesRouteDefinitionLocator propertiesRouteDefinitionLocator(GatewayProperties properties) {
        return new PropertiesRouteDefinitionLocator(properties);
    }

    @Bean
    @ConditionalOnMissingBean(RouterLocator.class)
    public RouterLocator routerLocator(List<RoutePredicateFactory> predicates, List<GatewayFilterFactory> filterFactories,
                                       RouteDefinitionLocator routeDefinitionLocator) {
        return new RouteDefinitionRouteLocator(gatewayProperties, routeDefinitionLocator, predicates, filterFactories);
    }

    @Bean
    @ConditionalOnMissingBean(RoutePredicateHandlerMapping.class)
    public RoutePredicateHandlerMapping routeHandlerMapping(GlobalCorsProperties globalCorsProperties, RouterLocator routerLocator) {
        return new RoutePredicateHandlerMapping(globalCorsProperties, environment, routerLocator);
    }

    @Bean
    @ConditionalOnMissingBean(RouteFilterHandlerAdapter.class)
    public RouteFilterHandlerAdapter routeHandlerAdapter(List<GlobalFilter> globalFilters) {
        return new RouteFilterHandlerAdapter(globalFilters, 60L);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
